package thread;

/**
 * 修改
 */
public class DeadLockDemo2 {
    private static Object chopsticks = new Object();//筷子
    private static Object spoon = new Object();//勺

    public static void main(String[] args) {
        Thread bf = new Thread() {
            public void run() {
                try {
                    System.out.println("北方人去吃饭...");
                    System.out.println("北方人去拿餐具...");
                    synchronized (chopsticks) {
                        System.out.println("北方人拿起了筷子!开始吃饭");
                        Thread.sleep(5000);
                        System.out.println("北方人吃完饭!");
                    }
                    System.out.println("北方人放下筷子，去拿勺子!");
                    synchronized (spoon) {
                        System.out.println("北方人拿起了勺子，开始喝汤!");
                        Thread.sleep(5000);
                        System.out.println("北方人喝了完汤");
                    }
                    System.out.println("北方人放下了勺!");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Thread nf = new Thread() {
            public void run() {
                try {
                    System.out.println("---南方人去吃饭...");
                    System.out.println("---南方人去拿餐具...");
                    synchronized (spoon) {
                        System.out.println("---南方人拿起了勺子，去喝汤!");
                        Thread.sleep(5000);
                        System.out.println("---南方人喝完汤");
                    }
                    System.out.println("---南方人放下勺子，去拿筷子!");
                    synchronized (chopsticks) {
                        System.out.println("---南方人拿起筷子，去吃饭!");
                        Thread.sleep(5000);
                        System.out.println("---南方人吃完饭！");
                    }
                    System.out.println("---南方人放下筷子!");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        bf.start();
        nf.start();
    }
}
